package vr;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;



class Main {
	private static void dbInit() throws SQLException, ClassNotFoundException
	{
//        Class.forName("org.h2.Driver");
//        Connection conn = DriverManager.
//            getConnection("jdbc:h2:~/test", "sa", "");
//        // add application code here
//        Statement stmt = conn.createStatement();
//        String sql = "CREATE TABLE IF NOT EXISTS PARTICIPANT" +
//                "(id INTEGER not NULL," +
//                " first VARCHAR(255), " + 
//                " last VARCHAR(255), " + 
//                " age INTEGER, " + 
//                " PRIMARY KEY ( id ))"; 
//        stmt.execute(sql);
//        sql = "INSERT INTO PARTICIPANT (id, first, last, age) VALUES" +
//                "('3', " +
//        		"'Johan', " +
//                " 'Stridkvist', " + 
//                " '32'); "; 
//        stmt.execute(sql);
//        sql = "SELECT * from PARTICIPANT;";
//        stmt.execute(sql);
//        ResultSet rs = stmt.executeQuery(sql);
//        while (rs.next()) {
//        	System.out.println(rs.getString("first") + rs.getString("last") + rs.getString("age"));
////        	String username = rs.getString("USERNAME");	
//        }
//        conn.close();
	}
	public static void main(String[] args) throws IOException,
			ClassNotFoundException, SQLException {
		dbInit();
		Logger.getGlobal().setLevel(Level.FINEST);
		Startlist sl = null;
		Results r = null;

		if (!Files.exists(Paths.get("StartList.xml"))) {
			sl = new Startlist();
			sl.generate(false);
			File file = new File("StartList.xml");
			FileOutputStream fis = new FileOutputStream(file);
			ObjectOutputStream writer = new ObjectOutputStream(fis);
			writer.writeObject(sl);
			writer.close();
		} else {
			File file = new File("StartList.xml");
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream reader = new ObjectInputStream(fis);
			sl = (Startlist) reader.readObject();
			reader.close();
		}

		File file = new File("results.xml");
		FileWriter writer = new FileWriter(file);
		r = new Results();
		r.getResults(sl);
		writer.write(r.toString());
		writer.close();

		
		System.out.println(r);
	}
}